import Vue from "vue";
import axios from "axios";
import Router from "vue-router";
import store from "../store/index.js";
Vue.use(Router);
// 2. 定义路由配置
const originalPush = Router.prototype.push;
const originalReplace = Router.prototype.replace;
//push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};
//replace
Router.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject);
  return originalReplace.call(this, location).catch((err) => err);
};
var router = new Router({
  routes: [
    {
      path: "",
      redirect: "/login",
    },
    {
      path: "/login",
      component: () => import("../views/login/index.vue"),
    },
    {
      path: "/assign",
      component: () => import("../views/assign/assign.vue"),
    },
    {
      path: "/activilist",
      component: () => import("../views/activityList/index.vue"),
    },
    {
      path: "/myActivity",
      component: () => import("../views/myActivity/index.vue"),
    },
    {
      path: "/myLove",
      component: () => import("../views/myLove/index.vue"),
    },
    {
      path: "/chat",
      component: () => import("../views/chat/index.vue"),
    },
    {
      path: "/activityDesc",
      component: () => import("../components/activityDesc/index.vue"),
    },
    {
      path: "/publish",
      component: () => import("../views/home/publish/index.vue"),
    },
    {
      path: "/home",
      component: () => import("../views/home/index.vue"),
      redirect: "/home/dashBoardd",
      children: [
        {
          path: "dashBoardd",
          component: () => import("../views/home/dashBoard/index.vue"),
        },
        {
          path: "chatList",
          component: () => import("../views/home/chatList/index.vue"),
        },
        {
          path: "mine",
          component: () => import("../views/home/mine/index.vue"),
        },
      ],
    },
  ],
  mode: "history",
});
router.beforeEach((to, from, next) => {
  // 得到本地存储的token  想要进其他页面，就要带token
  //lin是为了让系统一开始就能进去
  //jian是闪烁标题
  let token = localStorage.getItem("token");
  if (
    (store.state.yb_userid != "" && store.state.yb_userid != undefined) ||
    sessionStorage.getItem("lin") == "ok"
  ) {
    sessionStorage.setItem("lin", "nook");
    next();
  } else {
    if (to.path !== "/login") {
      sessionStorage.setItem("jian", "ok");
      next({ path: "/login" });
    } else {
      next();
    }
  }
});
export default router;
